var delay_hide_id;
if (Drupal.jsEnabled) {
  $(document).ready(function () {    
    $("input:text").bind("focus",  show_keyboard);
    $("input:password").bind("focus", show_keyboard);
    $("textarea").bind("focus", show_keyboard);
  });
  document.write("<div id='_divkeyboard' style='position:absolute; z-index:10; visibility: hidden; background-color:white;border:solid 1px #dddddd;top:10px;width:398px;height:74;padding:5px;'><div style='height:15px;width:100%;text-align:right;'><span class='kkeyboard'><a href='"+kwhelpurl+"' target='kasahorow'>"+Drupal.t(kwhelptext)+"</a></span>&nbsp;<img src='"+kwvkl+"/close.png' onclick='hide_keyboard(event);' style='width:11px;height:11px;cursor:pointer' alt='"+Drupal.t('Hide keyboard')+"' /></div><div id='_divkeyboardcontainer' style='width:100%;height:100%'></div></div>");
}

function get_element_left_position(element){
    var left=0;
    if(element.offsetParent){
            while(element.offsetParent)
            {
                    left+=element.offsetLeft;
                    element=element.offsetParent;
            }
            left+=element.offsetLeft;
    }
    else if(element.x){
            left+=element.x;
    }
    return left;
}

function get_element_top_position(element){
  var top=0;
  if(element.offsetParent){
          while(element.offsetParent)
          {
                  top+=element.offsetTop;
                  element=element.offsetParent;
          }
          top+=element.offsetTop;
  }
  else if(element.y){
          top+=element.y;
  }
  return top;
}
  
function delay_hide_keyboard(ev){
  delay_hide_id = window.setTimeout(hide_keyboard,500);
}

function cancel_delay_hide_keyboard(){
  if(delay_hide_id>=0)
  window.clearTimeout(delay_hide_id);
  delay_hide_id=-1;
}

function hide_keyboard(ev){
  VirtualKeyboard.hide();
  var kb = document.getElementById('_divkeyboard');
  kb.style.visibility="hidden";
  //Remove bindings to avoid conflict with bindings of VirtualKeyboard
  $("input:text").unbind("keydown");
  $("input:password").unbind("keydown");
  $("textarea").unbind("keydown");
  return false;
}


function show_keyboard(ev){
  //Binding our own keypress handler so we can remove our floating div
  $("input:text").bind("keydown", handle_keypress);
  $("input:password").bind("keydown", handle_keypress);
  $("textarea").bind("keydown", handle_keypress);
  ctrl = this;
  ev = ev || window.event;
  var target = ev.target || ev.srcElement;
  cancel_delay_hide_keyboard();
  var kb=document.getElementById('_divkeyboard');
  kb.style.visibility="visible";
  var left=get_element_left_position(target);
  var top=get_element_top_position(target);
  if((left+target.offsetWidth + kb.offsetWidth+5)>document.body.offsetWidth){
    top+=(target.offsetHeight + 5);
  }
  else{
    left+=target.offsetWidth+5;
    top-=5;
  }
  kb.style.top=top+"px";
  kb.style.left=left+"px";
  var isOpen = VirtualKeyboard.isOpen();
  if(!isOpen){
    VirtualKeyboard.show(ctrl.id,'_divkeyboard');
  }
  VirtualKeyboard.attachInput(ctrl);
  return false;
}

function handle_keypress(ev){
  ctrl = this;
  ev = ev || window.event;
  var keycode = window.event?ev.keyCode:ev.which;
  var target   = ev.target || ev.srcElement;
  if(keycode == 9){//overwrite how the tab key is handled.
    hide_keyboard(ev);
    return true;
  }else if(keycode == 27){//hide the keyboard when the escpae key is pressed
    hide_keyboard(ev);
    return false;
  }else if(keycode == 13){//The enter key has been hit
    //for non-textarea controls, force the enter key to submit the form.
    if(target.type.toUpperCase()!="TEXTAREA"){
        if(target && target.form.submit)
            target.form.submit();
    }
  }
  return false;
}
